import dynamicRouter from '@/router/dynamicRouter';
import store from '@/vuex/index';
import app from '@/main';
import {proxy, baseUrl} from './config';
import {ElMessage} from 'element-plus';
import router from '@/router/index';

const service = {
    //处理外发请求
    doRequest(url, method, data) {
        const headers = {};
        headers.Authorization = store.getters.getUserInfo.token;
        return new Promise((resolve, reject) => {
            app.axios({
                url: baseUrl + proxy + url,
                method,
                data,
                headers
            }).then((response) => {
                switch (response.status) {
                    case 200: {
                        const data = response.data;
                        if (data.code !== 1) {
                            ElMessage({
                                showClose: false,
                                message: data.msg,
                                type: 'warning',
                            });
                        }
                        resolve(data);
                        break;
                    }
                    case 401: {
                        ElMessage({
                            showClose: false,
                            message: '请登录',
                            type: 'error',
                        });
                        store.dispatch('handleSetIsLogin', false);
                        router.push('/login');
                        resolve({code: 0, msg: '未登录'});
                        break;
                    }
                }
            }).catch((err) => {
                if (err.response.status === 401) {
                    ElMessage({
                        showClose: false,
                        message: '请登录',
                        type: 'error',
                    });
                    store.dispatch('handleSetIsLogin', false);
                    router.push('/login');
                } else {
                    ElMessage({
                        showClose: false,
                        message: err.message,
                        type: 'error',
                    });
                }
                reject(err);
            });
        })
    },
    //执行post
    doPost(url, data) {
        return service.doRequest(url, 'POST', data);
    },
    //执行get
    doGet(url, data = {}) {
        const query = service.dealParams(data);
        return service.doRequest(url + query, 'GET', data);
    },
    //处理get请求参数
    dealParams(data) {
        let query = '';
        for (let key in data) {
            if (query) {
                query += '&' + key + '=' + data[key];
            } else {
                query = key + '=' + data[key];
            }
        }
        return query ? '?' + query : '';
    },
    //添加路由
    addRouter(router) {
        let addRoute = function (children, name = '') {
            for (let i = 0; i < children.length; i++) {
                let child = children[i];
                if (name) {
                    router.addRoute(name, {
                        path: child.path,
                        name: child.name,
                        component: child.component,
                        meta: child.meta
                    });
                } else {
                    router.addRoute({path: child.path, name: child.name, component: child.component, meta: child.meta});
                }
                if (child.children && child.children.length > 0) {
                    addRoute(child.children, child.name)
                }
            }
        };
        addRoute(dynamicRouter);
    }
};
export default service;